<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>黑马电商管理系统</span>
      </div>
      <el-button type="info" @click="logout"> 退出 </el-button>
    </el-header>
    <el-container>
      <!-- 侧边菜单导航 unique-opened只保持一个子菜单的展开-->
      <el-aside :width= "isCollapse ? '64px' : '200px' ">
        <!-- 折叠按钮 -->
        <div class="toggle-button" @click="changeCollapse">|||</div>
        <!-- 菜单 -->
        <el-menu
          background-color="#353c42"
          text-color="#fff"
          active-text-color="#409Eff"
          unique-opened
          :collapse = "isCollapse"
          :collapse-transition = "false"
          router
          :default-active="aPath"
        >
          <!-- 一级菜单 -->
          <!-- 使用动态属性index 值必须是字符串 toString() 把值改为字符串-->
          <el-submenu
            :index="item.id.toString()"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <!-- 根据菜单id取出同名的属性值 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <!-- 对应数组里面的resData 里面data authName -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级子菜单 子菜单循环 -->
            <!-- 使用动态属性index 值必须是字符串 toString() 把值改为字符串-->
            <el-menu-item
              :index="sonItem.path"
              v-for="sonItem in item.children"
              :key="sonItem.id"
              @click="cheoseAcivePath(sonItem.path)"
            >
              <!-- 二级菜单模板 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-sunny"></i>
                <!-- 文本 -->
                <span>{{ sonItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 中间区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>


<script>
export default {
  created() {
    //因为挂载到vue实例上面 所有this可以用this
    this.getMenuList()
    //根据本地缓存中的aPath
    this.aPath = window.sessionStorage.getItem('aPath')
  },
  data: function () {
    return {
      aPath:'',
      isCollapse:false,
      menuList: [],
      //一级菜单字体图标样式菜单id:样式类名
      iconsObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao',
      },
    }
  },
  methods: {
    cheoseAcivePath(aPath) {
      //通过设置给data 中的 aPath 来修改 菜单的 default-active属性来 
      this.aPath = aPath
      //将aPath保存到本地缓存
      window.sessionStorage.setItem('aPath',aPath)
      console.log(aPath);
    },
    changeCollapse () {
      this.isCollapse = ! this.isCollapse
    },
    logout() {
      this.$MessageBox.confirm('宝贝确定退出吗?', '来自宝宝的提醒', {
        //用户点击了确定退出按钮
        confirmButtonText: '确定啊!',
        callback: (action) => {
          if (action == 'confirm') {
            //删除sessionStorage (把token保存到浏览器的缓存里)
            window.sessionStorage.clear()
            //跳转到登录页面
            this.$router.push('/login')
          }
        },
      })
    },
    async getMenuList() {
      let { data: resData } = await this.$http.get('menus')
      // console.log(resData)
      //获取菜单成功
      if (resData.meta.status == 200) {
        //将返回的菜单数组 设置给vue实例的data.menuList
        this.menuList = resData.data
        console.log(resData)
      }
    },
    
  },
}
</script>

<style lang='less' scoped>
.el-header {
  background-color: #353c42;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #e9edf1;
}
.home-container {
  height: 100%;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>